import {Button, Col} from "antd";
import React, {DOMElement} from "react";

import {withAuth} from "../../Utils/AuthParsed";

// @ts-ignore
export const authBtn =(key?:string)=> key? withAuth(key,Button): Button;

interface SubmitBtnProps {
    span?:number;
    offset?:number;
}
// 表单的 提交按钮，支持功能权限(在 config/authorize.config.ts 配置)
export const ColFormSubmit:React.FC<SubmitBtnProps> = ({span,offset=0})=>
    <Col span={span} offset={offset}>
        <Button type="primary" htmlType="submit">查找</Button>
    </Col>;


interface CreateBtnProps extends SubmitBtnProps{
    onClick?:()=>null;
    createAuthKey?:string; // 授权的key
}
// 表格的 新增按钮，支持功能权限(在 config/authorize.config.ts 配置)
export const ColFormCreate:React.FC<CreateBtnProps> = ({span,offset=0,onClick=()=>null ,createAuthKey})=>{
    const Create = authBtn(createAuthKey);
    return (<Col span={span} offset={offset}>
                <Button type="primary" onClick={onClick}>新建</Button>
            </Col>);
};

export function download(blob:any, fileName:string):void{
    let it:HTMLElement = window.document.getElementById("forlink")|| new HTMLElement();
    let blobUrl = window.URL.createObjectURL(blob);
    const filename = fileName;
    it.setAttribute("href",blobUrl);
    it.setAttribute("download",filename);
    it.click();
    it.setAttribute("href","");
}

// url,会自动拼接 默认的 服务器地址
export function downloadUrl(url:string){
    if(!url.startsWith("http")){
        url = process.env.server+ url;
    }
    let it : HTMLElement = window.document.getElementById("forlink") || new HTMLElement();
    it.setAttribute("href",url);
    it.click();
    it.setAttribute("href","");
}
